
SOURCES     = $(shell ls  *.py | grep -v "_ui.py" | grep -v "_.py")
ENCODERS    = $(shell ls encoders/*.py)
FORMS       = $(shell ls *.ui encoders/*.ui)
TS_FILES    = $(shell ls translations/*.ts)
DESKTOP_TR_FILES = $(shell ls translations/*.desktop)
VERSION     := $(shell awk -F"'" '/FLACON_VERSION=/ {print($$2)}' flacon.py)

TRANSLATORS_FILES = $(shell ls translations/*_*.info)
TRANSLATORS_PY_FILE = translators_.py

LRELEASE    = lrelease
PYLUPDATE   = pylupdate4
DEL_FILE    = rm -f
DEL_DIR     = rm -rf
PYUIC       = pyuic4

DESTDIR     =
PREFIX      = $(DESTDIR)/usr
INST_DIR    = $(PREFIX)/share/flacon
TMP_DIR     = /tmp/flacon/flacon-$(VERSION)
DISTR_DIR   = $(HOME)
LINK_DIR    = $(PREFIX)/share/applications
ICON_DIR    = $(PREFIX)/share/icons/hicolor
TSPRO_FILE  = translations/flacon.pro

UI_PY_FILES = $(FORMS:.ui=_ui.py)
QM_FILES    = $(TS_FILES:.ts=.qm)

#***********************************************************

all: $(SOURCES) $(UI_PY_FILES) $(ENCODERS) $(QM_FILES) $(TRANSLATORS_PY_FILE)


%.qm: %.ts
	$(LRELEASE) $<  -qm $@


%_ui.py: %.ui
	@which $(PYUIC)
#	$(PYUIC) $< -o $@
#       Compatibility with old version of PyQt
	$(PYUIC) $< | \
	    sed  -e "s/\(\.setProperty(.*, \)\(False\))/\1QtCore.QVariant(\2))/" | \
	    sed  -e "s/\(\.setProperty(.*, \)\(True\))/\1QtCore.QVariant(\2))/"  | \
	    sed  -e "s/\(\.setProperty(.*, \)\([0-9][0-9]*\))/\1QtCore.QVariant(\2))/" > $@


$(TRANSLATORS_PY_FILE):$(TRANSLATORS_FILES) translations/make_translators.py
	python translations/make_translators.py $(TRANSLATORS_FILES) > $(TRANSLATORS_PY_FILE)


translates: $(TSPRO_FILE)
	echo SOURCES=$(SOURCES:%=../%) $(ENCODERS:%=../%) >> $(TSPRO_FILE)
	echo FORMS=$(FORMS:%=../%) >> $(TSPRO_FILE)
	echo TRANSLATIONS = $(TS_FILES:translations/%=%) >> $(TSPRO_FILE)
	$(PYLUPDATE) $(TSPRO_FILE)


clean:
	$(DEL_FILE) *.pyc   *.*~   *_ui.py *_.py
	$(DEL_FILE) encoders/*.pyc encoders/*.*~ encoders/*_ui.py 
	$(DEL_FILE) translations/*.qm translations/*.*~
	$(DEL_FILE) tests/*.*~ tests/*.pyc tests/data/*.*~
	$(DEL_FILE) $(TSPRO_FILE)


install:all
	install -d $(INST_DIR)
	install -m 644 $(SOURCES) $(UI_PY_FILES)  $(TRANSLATORS_PY_FILE) $(INST_DIR)
	install -m 755 flacon.py $(INST_DIR)

	install -d $(INST_DIR)/encoders
	install -m 644 $(ENCODERS) $(INST_DIR)/encoders/

	install -d $(INST_DIR)/translations
	install -m 644 $(QM_FILES) $(INST_DIR)/translations/

	install -d $(INST_DIR)/images
	install -m 644 ./images/* $(INST_DIR)/images/

	cd $(INST_DIR) && python -mcompileall .

	install -d $(PREFIX)/bin/
	install -m 755 misc/flacon $(PREFIX)/bin/

	install -d $(LINK_DIR)
	install -m 644 misc/flacon.desktop $(LINK_DIR)/flacon.desktop
	grep --no-filename "\[..]" translations/*.desktop >> $(LINK_DIR)/flacon.desktop

	install -d $(ICON_DIR)/16x16/apps $(ICON_DIR)/32x32/apps $(ICON_DIR)/48x48/apps
	install -m 644 misc/flacon-16x16.png $(ICON_DIR)/16x16/apps/flacon.png
	install -m 644 misc/flacon-32x32.png $(ICON_DIR)/32x32/apps/flacon.png
	install -m 644 misc/flacon-48x48.png $(ICON_DIR)/48x48/apps/flacon.png
	$(DEL_FILE) $(INST_DIR)/encoder.py*


uninstall:
	$(DEL_DIR) $(INST_DIR)
	$(DEL_FILE) $(PREFIX)/bin/flacon
	$(DEL_FILE) $(LINK_DIR)/flacon.desktop
	$(DEL_FILE) $(ICON_DIR)/16x16/apps/flacon.png
	$(DEL_FILE) $(ICON_DIR)/32x32/apps/flacon.png
	$(DEL_FILE) $(ICON_DIR)/48x48/apps/flacon.png


copysrc:
	rm -rf "$(TMP_DIR)/"*
	install -d "$(TMP_DIR)"

	install -m 644 $(SOURCES) *.ui ./Makefile "$(TMP_DIR)/"
	install -m 755 flacon.py "$(TMP_DIR)/flacon.py"
	echo $(FORMS)

	install -d "$(TMP_DIR)/encoders"
	install -m 644 $(ENCODERS) encoders/*.ui "$(TMP_DIR)/encoders/"

	install -d "$(TMP_DIR)/translations/"
	install -m 644 $(TS_FILES) $(DESKTOP_TR_FILES) translations/*.ts.src  "$(TMP_DIR)/translations/"
	install -m 644 translations/*.info "$(TMP_DIR)/translations/"
	install -m 644 translations/*.py "$(TMP_DIR)/translations/"

	install -d "$(TMP_DIR)/misc"
	install -m 644 ./misc/* "$(TMP_DIR)/misc/"
	install -m 755 ./misc/flacon "$(TMP_DIR)/misc/"

	install -d "$(TMP_DIR)/images"
	install -m 644 ./images/* "$(TMP_DIR)/images/"

	install -d "$(TMP_DIR)/distr/deb/debian"
	install -m 644 distr/deb/README  "$(TMP_DIR)/distr/deb/"
	install -m 755 distr/deb/makeDeb.sh  "$(TMP_DIR)/distr/deb/"
	install -m 644 distr/deb/debian/*  "$(TMP_DIR)/distr/deb/debian/"

	install -d "$(TMP_DIR)/tests/"
	install -m 755 tests/flacon_tests.py  "$(TMP_DIR)/tests"
	install -d "$(TMP_DIR)/tests/data/"
	install -m 644 tests/data/*  "$(TMP_DIR)/tests/data/"


distr: copysrc
	cd "$(TMP_DIR)" && make
	cd "$(TMP_DIR)"/.. && tar -czf "$(DISTR_DIR)/flacon-$(VERSION).tgz" "flacon-$(VERSION)"
	rm -rf "$(TMP_DIR)"

